from flask import Blueprint, request
from flask_restful import Resource, Api
from models.user import User

# 创建蓝图对象
user_bp = Blueprint('user', __name__)

# @user_bp.route('/info', methods=['GET', 'POST'])
# def info():
#     """
#     获取用户信息
#     :return:
#     """
#     if request.method == 'GET':
#         return 'This is GET request'
#     elif request.method == 'POST':
#         return 'This is POST request'
#     else:
#         return 'user info'

# 使用flask-restful改造成视图类的形式

# 创建api对象，并跟蓝图对象进行关联
api = Api(user_bp)

class Index(Resource):
    def get(self):
        mobile = request.args.get('mobile')
        user = User.query.filter(User.mobile == '13021074747').first()
        data = {
            'user_id': user.id,
            'user_name': user.name,
            'mobile': user.mobile
        }
        return data

    def post(self):
        return 'This is POST request'

api.add_resource(Index, '/info')
